/**
 * 
 */
package io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import game.BuildingType;

/**
 * @author Remi REBILLARD
 *
 */
public class ImportBuildingType {
	private BufferedReader buffer;
	
	private String header;
	private String[] content;
	
	/**
	 * @param inputstream
	 */
	public ImportBuildingType( InputStream is ){
		this.buffer = new BufferedReader( new InputStreamReader( is ) );
	}
	
	/**
	 * @return the data of file for BuildingType
	 */
	public HashMap< String, BuildingType > read() throws IOException{
		
		HashMap< String, BuildingType > data = new HashMap< String, BuildingType >();
		header = buffer.readLine();
		
		if( !header.equals( "game_db_buildings\t1" ) )
			throw new IOException( "Invalid Header of BuidlingType" );
		
		while( buffer.ready() ){
			
			content = buffer.readLine().split( "\t" );
			BuildingType tmpBuild = new BuildingType
			(
				content[0],							//NAME
				Integer.parseInt( content[6] ),		//DEFENSE
				Boolean.parseBoolean( content[7] )	//ISESSENTIAL
			);
			
			tmpBuild.setImage( 0, content[1] ); //GREY
			tmpBuild.setImage( 1, content[2] );	//RED
			tmpBuild.setImage( 2, content[3] );	//BLUE
			tmpBuild.setImage( 3, content[4] );	//GREEN
			tmpBuild.setImage( 4, content[5] );	//YELLOW
			
			data.put( content[0], tmpBuild );
		}
		return data;
	}
}
